package com.ruoyi.web.controller.website;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.module.domain.Material;
import com.ruoyi.module.domain.Points;
import com.ruoyi.module.service.IMaterialService;
import com.ruoyi.module.service.IPointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 素材库内容Controller
 *
 * @author fubojin
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/website/material")
public class WebMaterialController extends BaseController
{
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private IPointsService pointsService;

    /**
     * 查询素材库内容列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Material material)
    {
        startPage();
        List<Material> list = materialService.selectMaterialList(material);
        System.out.println("=========");
        return getDataTable(list);
    }

    /**
     * 导出素材库内容列表
     */
    @Log(title = "素材库内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Material material)
    {
        List<Material> list = materialService.selectMaterialList(material);
        ExcelUtil<Material> util = new ExcelUtil<Material>(Material.class);
        util.exportExcel(response, list, "素材库内容数据");
    }

    /**
     * 获取素材库内容详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(materialService.selectMaterialById(id));
    }

    /**
     * 新增素材库内容
     */
    @Log(title = "素材库内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Material material)
    {
        return toAjax(materialService.insertMaterial(material));
    }

    /**
     * 修改素材库内容
     */
    @Log(title = "素材库内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Material material)
    {
        return toAjax(materialService.updateMaterial(material));
    }

    /**
     * 审核通过
     */
    @Log(title = "素材库内容", businessType = BusinessType.UPDATE)
    @PutMapping("/auditApprove/{id}")
    public AjaxResult auditApprove(@PathVariable("id") Long id, @RequestParam Integer pointsQuantity)
    {
        Material material = materialService.selectMaterialById(id);
        material.setAuditStatus(2);

        //添加该用户积分
//        Points points1 = pointsService.selectPointsByUserId(material.getUserId());
            Points points = new Points();
            points.setType(material.getType());
            points.setUserId(material.getUserId());
            points.setPointsQuantity(pointsQuantity);
            pointsService.insertPoints(points);

        return toAjax(materialService.updateMaterial(material));
    }
    /**
     * 审核不通过
     */
    @Log(title = "素材库内容", businessType = BusinessType.UPDATE)
    @PutMapping("/auditReject/{id}")
    public AjaxResult auditReject(@PathVariable("id") Long id)
    {
        Material material = materialService.selectMaterialById(id);
        material.setAuditStatus(2);
        return toAjax(materialService.updateMaterial(material));
    }

    /**
     * 删除素材库内容
     */
    @Log(title = "素材库内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(materialService.deleteMaterialByIds(ids));
    }
}
